<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>厂商列表</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<!-- 是否显示启用禁用按钮 -->
<sys:funcAuth resCd="disable">
	<span id="isDisableBtn" />
</sys:funcAuth>
<!-- 是否显示修改按钮 -->
<sys:funcAuth resCd="modify">
	<span id="isModifyBtn" />
</sys:funcAuth>
<!-- 是否显示删除按钮 -->
<sys:funcAuth resCd="delete">
	<span id="isDeleteBtn" />
</sys:funcAuth>
<!-- 是否显示新增厂商按钮 -->
<sys:funcAuth resCd="add">
	<span id="isAddBtn" />
</sys:funcAuth>
<!-- 是否显示备案账号按钮 -->
<sys:funcAuth resCd="mfrBankAcc">
	<span id="isBankAccBtn"/>
</sys:funcAuth>
<!-- 是否显示基本信息变更按钮按钮 -->
<sys:funcAuth resCd="mfrBaseMod">
	<span id="isBaseModBtn"/>
</sys:funcAuth>
<!-- 是否显示付款方式变更按钮 -->
<sys:funcAuth resCd="mfrPayTypMod">
	<span id="isPayTypModBtn"/>
</sys:funcAuth>
<!-- 是否显示发起准入按钮 -->
<sys:funcAuth resCd="mfrAdmission">
	<span id="isAdmissionBtn"/>
</sys:funcAuth>

<script type="text/javascript">
	var grid = null;
	var pageInApp={}; // to 下一级子窗口 接口
	var pageInTab=null;
	var currentOrgCd = '${currentOrgCd}';
	$(function () {
		initTab();
		
	    $("#ligergrid1").ligerGrid({
	    	url: '<%=path%>/mfr/mfrBscInfo/initList.do',
	    	method : 'get',
	        columns: [
	            { display: '厂商名称',align: 'left',width: 250, 
		            render: function (rowdata, rowindex, value){
	            		var h='<a href="javascript:toMfrDetail(';
	            		h+="'"+rowdata.id+"',";
	            		h+="'"+rowdata.audStsCd+"'";
	                    h+=')">'+rowdata.mfrName+'</a>'
	                return h;
            	}
	            },
	            { display: '厂商传真', name: 'mfrFax', width: 150 },
	            { display: '厂商电话', name: 'mfrTel', width: 150},
	            { display: '使用状态', name: 'isValidNm', width: 120 },
                { display: '放款方式', name: 'payTypCdNm', width: 120 },
	            { display: '审核状态', name: 'audStsCdNm', width: 120 },
				getOptColumns(),
	        ],
	        pageSize:10,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        //isScroll: false,
	        checkbox:true,
	        rownumbers:true,
	        /* onRenderComplete: function () {
	        	iframeResize ();
	        } */
	        onAfterShowData: function () {
	            $(".gridTip").grid_ligerTip();
	            SetIframe_height ();
	        }
	    });
	    grid = liger.get("ligergrid1");
	    grid.setHeight(100);
	    $("#searchBar").ligerForm();
	
	    $("#searchBtn").click(function(event) {
	         var form = new liger.get("searchBar");
	         var data = form.getData();
	         data["crtTm"] = $("#crtTm").val();
	         //alert(JSON.stringify(data));
	         for (name in data){
	            grid.setParm(name,data[name]);
	         };
	        grid.reload();
	    });
	    $("#clearBtn").click(function(event) {
	        $("#resetBtn").click();  //重置 input-text
	        var form = liger.get("searchBar");
	        form.setData({  //重置select等特殊表单
	            "orgCd": "-1"
	        });
	    });
	    
	    $("#pageloading").hide();
	    addButtonShow();
	});

	function getOptColumns(){
		var col =   { display: '操作', width: 300 ,align:'left',
					render: function (rowdata, rowindex, value){
						var opt = "";
						var v;
						var v1='<a class="sq-button-hasicon" href="javascript:;" onclick="disable(';
						v1+="'"+rowdata.id+"'";
						v1+=')"><i class="sqBtnIco_13"></i>'+LogUtils.LOG_CMM_GRID_BTN_DISABLE+'</a>';

						var v2='<a class="sq-button-hasicon" href="javascript:;" onclick="enable(';
						v2+="'"+rowdata.id+"'";
						v2+=')"><i class="sqBtnIco_12"></i>'+LogUtils.LOG_CMM_GRID_BTN_ENABLE+'</a>';
						switch (rowdata.isValid){
							case 0:
								v=v2;
								break;
							case 1:
								v=v1;
								break;
							default:
								v=v1;
								break;
						}
						var h='<a class="sq-button-hasicon" href="javascript:toMfrModify(';
						h+="'"+rowdata.id+"'";
						h+=')"><i class="sqBtnIco_4"></i>修改</a>';
						var d='<a class="sq-button-hasicon" href="javascript:deleteRecord(';
						d+="'"+rowdata.id+"'";
						d+=')"><i class="sqBtnIco_7"></i>删除</a>';

						var j = '<a class="sq-button-hasicon" href="javascript:toMfrBaseModPage(';
						j += "'"+rowdata.id+"'";
						j += ')"><i class="sqBtnIco_4"></i>基本信息变更</a>';
						var f = '<a class="sq-button-hasicon" href="javascript:toMfrPayTypModPage(';
						f += "'"+rowdata.id+"'";
						f += ')"><i class="sqBtnIco_4"></i>放款方式变更</a>';
						var b = '<a class="sq-button-hasicon" href="javascript:toMfrBankPage(';
						b += "'"+rowdata.id+"'";
						b += ')"><i class="sqBtnIco_4"></i>备案账号</a>';
						var z = '<a class="sq-button-hasicon" href="javascript:startAdmissionFlow(';
						z += "'"+rowdata.id+"','" + rowdata.audStsCd + "'";
						z += ')"><i class="sqBtnIco_4"></i>发起准入申请</a>';

						var isDisableBtn = $("#isDisableBtn");
						var modify = $("#isModifyBtn");
						var deletes = $("#isDeleteBtn");
						var bankAcc = $("#isBankAccBtn");
						var baseMod = $("#isBaseModBtn");
						var isPayTypModBtn = $("#isPayTypModBtn");
						var isAdmissionBtn = $("#isAdmissionBtn");
						var end = "";
						var _audStsCd = rowdata.audStsCd;
						if(isAdmissionBtn.length && _audStsCd == 0 || _audStsCd == 3){
							end += z;
						}
						if(baseMod.length && _audStsCd == 2 || _audStsCd == 12){//11:基本信息变更中,12:放款方式变更中,13:同时进行基本信息变更和放款方式变更
							end += j;
						}
						if(isPayTypModBtn.length && _audStsCd == 2 || _audStsCd == 11){
							end += f;
						}
						if(bankAcc.length && _audStsCd != 3){
							end += b;
						}
						if (modify.length && _audStsCd != 2 && _audStsCd != 11 && _audStsCd != 12 && _audStsCd!=1 && _audStsCd != 13) {
							opt += h;
						}
						if (deletes.length && _audStsCd != 3) {
							opt += d;
						}
						if (isDisableBtn.length && (_audStsCd != 0 || _audStsCd != 3)) {
							opt += v;
						}
						return opt + SQ.dropLinks(end);
					}
				};
		return col;
	}

	var callback = function () {
		grid.reload();
    };
	 //新增数据
	function add(){
		if (StringUtils.isEmpty(currentOrgCd) || '0000' == currentOrgCd) {
			currentOrgCd = $('#buOrgCd').val();
			console.log(currentOrgCd);
			if (StringUtils.isEmpty(currentOrgCd)) {
				wait_dialog=$.ligerDialog.SQalert({content: "请选择事业部",SQ_type:"warning"});
				return ;
			}
		}
		var params = '?currentOrgCd=' + currentOrgCd;
		currentOrgCd = '${currentOrgCd}';
        Add_navTab("PrdMfr_1"+Date.parse(new Date()), "新增厂商", '<%=path%>/mfr/mfrBscInfo/toAddPage.do' + params, callback);
	}
	//删除数据
	function deleteRecord(id){
		if(!id)return;
		var _ids = [];
		_ids.push(id);
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM+"厂商下产品也会级联删除！", function (flag) {
			if(flag == false)return;
			var wait_dialog;
			$.ajax({
				type: 'post', 
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/mfr/mfrBscInfo/removeByIds.do',
				beforeSend: function(){
			          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
			     },
				success:function(msg){
					wait_dialog.close();
					 if(msg.success == 1){
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
						 grid.reload();
					 }else{
						 $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
					 }
				}
			});
		});
	}
	
	//发起审核流程
    function startAdmissionFlow(_id,_auditState){
        if(!_id)return;
        if(StringUtils.isNotEmpty(_auditState) && (_auditState == '11' || _auditState == '12' || _auditState == '13')){
            $.ligerDialog.SQalert({content:"该厂商存在变更流程，请变更流程结束后再发起审核流程.", SQ_type: "error"});
            return;
        }
        var modelTyp = "043";//流程业务分类
        if(StringUtils.isEmpty(modelTyp)){
            console.log("modelTyp is null");
            return;
        }

        var reason = $('#reason').val();
        var flowDialog = $.ligerDialog.SQopen({
            url: '<%=path%>/flow/flow/toForStartListPage.do?modelTyp='+modelTyp,
            width: 700,
            height:400,
            title : '请选择要发起的流程',
            buttons: [{
                text: "确定", type: 'ok', cls: 'l-dialog-btn-ok',
                onclick: function (item, Dialog) {
                    if (Dialog.onOk) {
                        $.ligerDialog.confirm('是否确定发起评审流程?', function (flag) {
                            if(flag == false) {
                                return;
                            }else{
                                Dialog.onOk();
                            }
                        })
                    }
                }
            }, {
                text: "取消", type: 'cancel', cls: 'l-dialog-btn-no',
                onclick: function (item, Dialog) {
                    if (Dialog.onCancel) Dialog.onCancel();
                }
            }
            ],
            onClose: function () {
                var json = flowDialog._DATA;
                console.log(json);
                if(!json){
                    return;
                }
                var wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting",onClose: function () {}});
                $.ajax({
                    type: 'post',
                    dataType:'json',
                    data:{
                        id:_id,
                        procDefKey:json.procDefKey,
                        procDefId:json.procDefId,
                        procNo:json.procNo,
                        reason:reason
                    },
                    url: '<%=path%>/product/mfrInfo/addMfrFlowStart.do',
                    success:function(msg){
                        wait_dialog.close();
                        if(msg.success == 1){
                            $.ligerDialog.SQalert({content:msg.info, SQ_showButtons: true,SQ_type: "success",SQ_onBtnclick:function(){grid.reload();}});
                        }else{
                            UIUtils.alertErrorHold1(msg.info);
                        }
                    }
                });
            }
        });
    }
	
	function addButtonShow (){
		var add = $("#isAddBtn");
		if (add.length == 0) {
			document.getElementById('addBtn').style.display="none";
		}
	}
	
	function iframeResize () {
	    if (parent.mainApp) {
	        parent.mainApp.parentTab.onResize();
	    };
	}
	function toMfrDetail(_id, _audStsCd){
		if(!_id || _id=="")return;
		if(!_audStsCd || _audStsCd=="")return;
		if (StringUtils.isEmpty(currentOrgCd) || '0000' == currentOrgCd) {
			currentOrgCd = $('#buOrgCd').val();
			console.log(currentOrgCd);
			if (StringUtils.isEmpty(currentOrgCd)) {
				wait_dialog=$.ligerDialog.SQalert({content: "请选择事业部",SQ_type:"warning"});
				return ;
			}
		}
		var params = '?id=' + _id + '&audStsCd=' + _audStsCd + '&currentOrgCd=' + currentOrgCd+'&isMask=1';
		currentOrgCd = '${currentOrgCd}';
		parent.mainApp.f_addTab('PrdMfr_3'+_id,'厂商详情','<%=path%>/mfr/mfrBscInfo/toDetailPage.do' + params);
	}
	var callback = function () {
		grid.reload();
    };
	function toMfrModify(_id){
		if(!_id || _id=="")return;
		if (StringUtils.isEmpty(currentOrgCd) || '0000' == currentOrgCd) {
			currentOrgCd = $('#buOrgCd').val();
			console.log(currentOrgCd);
			if (StringUtils.isEmpty(currentOrgCd)) {
				wait_dialog=$.ligerDialog.SQalert({content: "请选择事业部",SQ_type:"warning"});
				return ;
			}
		}
		var params = '?id='+_id + '&currentOrgCd=' + currentOrgCd;
		currentOrgCd = '${currentOrgCd}';
		Add_navTab('PrdMfr_2'+_id, "修改厂商", '<%=path%>/mfr/mfrBscInfo/toAddPage.do' + params, callback);
	}
	
	function toMfrBaseModPage(_id){
		if(!_id || _id=="")return;
		var params = '?id=' + _id + '&isMdfPayTyp=0&isModify=1';
		Add_navTab('mfr_basc_mod'+_id, "基本信息变更", '<%=path%>/mfr/mfrBscInfo/toMfrModPage.do' + params, callback);
	}
	
	function toMfrPayTypModPage(_id){
		if(!_id || _id=="")return;
		var params = '?id=' + _id + '&isMdfPayTyp=1&isModify=0';
		Add_navTab('mfr_pay_typ_mod'+_id, "放款方式变更", '<%=path%>/mfr/mfrBscInfo/toMfrModPage.do' + params, callback);
	}
	
	function toMfrBankPage(_id){
		if(!_id || _id=="")return;
		Add_navTab('mfr_bank_acc_'+_id, "备案帐号", '<%=path%>/mfr/mfrbankacc/toListPage.do?mfrId='+_id, callback);
	}
	//启用
	function enable(id){
		if(!id)return;
		$.ligerDialog.confirm(LogUtils.LOG_CMM_ENABLE_CONFIRM, function (flag) {
   			if(flag == false)return;
   			var wait_dialog;
   			$.ajax({
   				type: 'post',
   				dataType:'json',
   				data:{id:id},
   				url: '<%=path%>/mfr/mfrBscInfo/enable.do',
   				beforeSend: function(){
   			     wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING,SQ_type:"waitting"});
   			    },
   				success:function(msg){
   					wait_dialog.close();
   					 if(msg.success == 1){
   						$.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
   						 grid.reload();
   					 }else{
   						$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
   					 }
   				}
   			});
   		});
	}
	//禁用
	function disable(id){
		if(!id)return;
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DISABLE_CONFIRM, function (flag) {
   			if(flag == false)return;
   			var wait_dialog;
   			$.ajax({
   				type: 'post',
   				dataType:'json',
   				data:{id:id},
   				url: '<%=path%>/mfr/mfrBscInfo/disable.do',
   				beforeSend: function(){
   			     wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING,SQ_type:"waitting"});
   			},

   				success:function(msg){
   					wait_dialog.close();
   					 if(msg.success == 1){
   						$.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
   						 grid.reload();
   					 }else{
   						$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
   					 }
   				}
   			});
   		});
	}
	
	var loadedTabs = [];
	//初始化Tab页签
	function initTab(){
		pageInTab = $("#pageInTab").ligerTab({
			height: 'auto',
			contextmenu: false,
			minIframeHeight: GET_minIframeHeight(),
			onBeforeSelectTabItem: function(tabid) {
				var selectedRows = grid.getSelectedRows();
				if(tabid!="tabA"){
					if (selectedRows.length != 1) {
			             $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
			             return false;
			         }
				}
			},
			onAfterSelectTabItem: function(tabid) {
				
				var selectedRows = grid.getSelectedRows();
				if(tabid!="tabA"){
					if (selectedRows.length != 1) {
			             $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warning"});
			             return false;
			         }else{
							if (loadedTabs.indexOf(selectedRows[0].id)<0) {
								loadedTabs = [];
								loadedTabs.push(selectedRows[0].id);
								console.log(tabid);
								switch (tabid){
									case "tabB":
										console.log(tabid)
										pageInTab.setTabItemSrc(tabid, "<%=path%>/mfr/mfrBscInfo/toMfrFlowPage.do?mfrId="+selectedRows[0].id);
										break;
								}
							}
							
							
							SetIframe_height();
						}
				}
				
			}
		});
		$.extend(true, pageInApp, {
            pageInTab: pageInTab
        });
		
	};
	
</script>

<body class="iframeBody">
<div class="Wrapper">
	<div id="pageInTab" class="pageInTab">
		<div tabid="tabA" title="项目列表" lselected="true">
			<div class="searchArea">
    			<form id="searchBar">
        			<div class="fieldset1 clearFix">
            			<div class="fieldBox">
               				<div class="label">厂商名称</div>
                			<div class="field">
                    			<input name="mfrNm" id="mfrNm" type="text" class="ui-textbox" value="">
                			</div>
            			</div>
           				<div class="fieldBox">
               				<div class="label">厂商地址</div>
                			<div class="field">
                    			<input name="mfrAddr" id="mfrAddr" type="text" class="ui-textbox" value="">
                			</div>
            			</div>
            			<c:if test="${currentOrgCd == '0000' }">
							<div class="fieldBox">
								<div class="label">所属事业部</div>
								<div class="field">
									<select name="buOrgCd" id="buOrgCd" data-value=" ">
										<option value=" " seleted>请选择</option>
										<c:forEach items="${searchBuOrgList }" var="kv">
											<option value="${kv.key }">${kv.value }</option>
										</c:forEach>
									</select>
								</div>
							</div>
						</c:if>
            		<div class="clearFix"></div>
            		<div class="btnsBox">
						<span id="searchBtn" class="sq-button sq-button-round">查询</span>
						<span id="clearBtn" class="sq-button sq-button-round">清空</span>
						<span id="addBtn" class="sq-button sq-button-round" onclick="add();">新增厂商</span>
						<input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
            		</div>
        		</form>
    		</div>
		</div>
		<div id="ligergrid1" class="sq-grid"></div>
	</div>
	<div tabid="tabB" title="流程跟踪" id="tabBId">
		<iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
	</div>

</body>
</html>